﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Common;
using DblogStudio.GUI.Utils;
using DblogStudio.Utils;

namespace DblogStudio.GUI.DuLieu
{
    public partial class FormQuanLyCongDoanSanXuat_ChiTiet : BaseDialog
    {
        public event TListChangedEventHandler OnChanged;
        CongDoanSanXuat congDoanSanXuat;
        int countEntityAdded = 0;
        public FormQuanLyCongDoanSanXuat_ChiTiet(CongDoanSanXuat coSoDaoTao = null)
        {
            InitializeComponent();
            this.congDoanSanXuat = coSoDaoTao;
            InitInterface();
        }

        void InitInterface()
        {
            if (congDoanSanXuat != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.CONGDOANSANXUAT);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtTenCongDoan.Text = congDoanSanXuat.TenCongDoanSanXuat;
                txtGhiChu.Text = congDoanSanXuat.GhiChu;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.CONGDOANSANXUAT);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtTenCongDoan.Text = "";
                txtGhiChu.Text = "";
            }
            txtTenCongDoan.Focus();
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtTenCongDoan.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTenCongDoan, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.CONGDOANSANXUAT));
                txtTenCongDoan.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (congDoanSanXuat != null)
                {
                    #region Cập nhật thông tin CSĐT
                    congDoanSanXuat.TenCongDoanSanXuat = txtTenCongDoan.Text;
                    congDoanSanXuat.GhiChu = txtGhiChu.Text;
                    var rs = BizCongDoanSanXuat.Update(congDoanSanXuat);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenCongDoan, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.CONGDOANSANXUAT));
                        txtTenCongDoan.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.CONGDOANSANXUAT));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới CSĐT
                    congDoanSanXuat = new CongDoanSanXuat();
                    congDoanSanXuat.TenCongDoanSanXuat = txtTenCongDoan.Text;
                    congDoanSanXuat.GhiChu = txtGhiChu.Text;
                    var rs = BizCongDoanSanXuat.Add(congDoanSanXuat);
                    congDoanSanXuat = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        OnChanged(this, new EntityArgs(congDoanSanXuat));
                        countEntityAdded++;
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenCongDoan, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.CONGDOANSANXUAT));
                        txtTenCongDoan.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.CONGDOANSANXUAT));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyCoSoDaoTao_ChiTiet_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
    }
}
